//
//  FMToast.m
//  WOM
//
//  Created by leeqb on 3/23/14.
//  Copyright (c) 2014 cst. All rights reserved.
//

#import "FMToast.h"

static FMToast *g_toastInstance = nil;

@implementation FMToast

- (id)initWithMessage:(NSString *)message
{
    self = [super init];
    if(self) {
        _message = message;
        CGRect rect;
        rect.origin.x = 0;
        rect.origin.y = SCREEN_TOP_SPACE + SCREEN_TOP_SPACE_REVERSE;
        rect.size.width = SCREEN_WIDTH;
        rect.size.height = SCREEN_HEIGHT - rect.origin.y;
        self.frame = rect;
        
        CGRect fontRect = [_message boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-80, SCREEN_HEIGHT)
                                                 options:NSStringDrawingUsesLineFragmentOrigin
                                              attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]}
                                                 context:nil];
        CGSize fontSize = fontRect.size;
        
        rect.size.width = fontSize.width + 40;
        rect.size.height = fontSize.height + 20;
        rect.origin.x = (SCREEN_WIDTH - rect.size.width) / 2;
        rect.origin.y = (SCREEN_HEIGHT - rect.size.height) / 2 - self.frame.origin.y;
        UIView *messageView = [[UIView alloc] initWithFrame:rect];
        messageView.alpha = 0.8;
        messageView.backgroundColor = [UIColor blackColor];
        messageView.layer.cornerRadius = 6;
        messageView.layer.shadowRadius = 6;
        messageView.layer.shadowColor = [UIColor blackColor].CGColor;
        messageView.layer.shadowOffset = CGSizeMake(1, 1);
        messageView.layer.shadowOpacity = 0.3;
        [self addSubview:messageView];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
        [self addGestureRecognizer:tapGesture];
        
        rect = messageView.frame;
        UILabel *messageLabel = [[UILabel alloc] initWithFrame:rect];
        messageLabel.text = _message;
        messageLabel.font = [UIFont systemFontOfSize:13];
        messageLabel.textColor = [UIColor whiteColor];
        messageLabel.backgroundColor = [UIColor clearColor];
        messageLabel.textAlignment = NSTextAlignmentCenter;
        messageLabel.numberOfLines = 0;
        messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [self addSubview:messageLabel];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [FMToast hide];
        });
    }
    return self;
}

- (id)initWithLoading:(NSString *)message
{
    self = [super init];
    if(self)
    {
        _message = message;
        CGRect rect;
        rect.origin.x = 0;
        rect.origin.y = SCREEN_TOP_SPACE + SCREEN_TOP_SPACE_REVERSE;
        rect.size.width = SCREEN_WIDTH;
        rect.size.height = SCREEN_HEIGHT - rect.origin.y;
        self.frame = rect;
        
        rect.size.width = 150;
        rect.size.height = 120;
        rect.origin.x = (SCREEN_WIDTH - rect.size.width) / 2;
        rect.origin.y = (SCREEN_HEIGHT - rect.size.height) / 2 - self.frame.origin.y;
        UIView *messageView = [[UIView alloc] initWithFrame:rect];
        messageView.alpha = 0.8;
        messageView.backgroundColor = [UIColor blackColor];
        messageView.layer.cornerRadius = 6;
        messageView.layer.shadowRadius = 6;
        messageView.layer.shadowColor = [UIColor blackColor].CGColor;
        messageView.layer.shadowOffset = CGSizeMake(1, 1);
        messageView.layer.shadowOpacity = 0.5;
        [self addSubview:messageView];
        
        rect = messageView.frame;
        rect.origin.y -= 15;
        UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:rect];
        indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        [self addSubview:indicatorView];
        [indicatorView startAnimating];
        
        rect = indicatorView.frame;
        rect.origin.y += 90;
        rect.size.height = 20;
        UILabel *messageLabel = [[UILabel alloc] initWithFrame:rect];
        messageLabel.text = _message;
        messageLabel.font = [UIFont systemFontOfSize:14];
        messageLabel.textColor = [UIColor whiteColor];
        messageLabel.backgroundColor = [UIColor clearColor];
        messageLabel.textAlignment = NSTextAlignmentCenter;
        messageLabel.numberOfLines = 0;
        messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [self addSubview:messageLabel];
    }
    return self;
}

- (void)tapHandle:(UITapGestureRecognizer *)tapGesture
{
    [FMToast hide];
}

+ (void)hide
{
    g_toastInstance.alpha = 0;
    //[g_toastInstance removeFromSuperview];
    //g_toastInstance = nil;
}

+ (FMToast *)showMessage:(NSString *)message
{
    if(message && ![message isEqual:[NSNull null]] && ![message isEqualToString:@""]) {
        [FMToast hide]; // 显示之前先隐藏掉上一个
        g_toastInstance = [[FMToast alloc] initWithMessage:message];
        
        UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
        [keyWindow addSubview:g_toastInstance];
    }
    return g_toastInstance;
}

+ (FMToast *)showLoading:(NSString *)message
{
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [FMToast hide];
    if(message && ![message isEqual:[NSNull null]] && ![message isEqualToString:@""]) {
        g_toastInstance = [[FMToast alloc] initWithLoading:message];
    } else {
        g_toastInstance = [[FMToast alloc] initWithLoading:@"努力加载中..."];
    }
    [keyWindow addSubview:g_toastInstance];
    return g_toastInstance;
}

@end
